package com.hieu.tripmanager;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hieu.abstracts.TripActivity;
import com.hieu.adapter.ActionAdapter;
import com.hieu.bean.Action;
import com.hieu.sql.DAL;
import com.hieu.util.Util;

public class ViewDayActivity extends TripActivity implements OnClickListener {
	private ListView listAction;
	private ArrayList<Action> mActionList;
	private ActionAdapter adapter;
	private TextView date;
	private TextView sum;
	private TextView des;
	private TextView amount;
	private TextView time;
	private DAL mDAL;
	private String day;
	private Util util = new Util();
	private ImageView img1, img2, img3;

	protected void initData() {
		// Kiểm tra có chuyến kết thúc không

		mDAL = new DAL(this);
		mActionList = new ArrayList<Action>();
		Intent intent = getIntent();
		if (intent != null) {
			day = intent.getStringExtra("Day");
		}

		if (mDAL != null) {
			mActionList = mDAL.getActionList(day);
		}

	}

	protected void initRootView() {
		setContentView(R.layout.view_day_activity_layout);
	}

	protected void initUIComponents() {
		// Add header to Listview
		listAction = (ListView) findViewById(R.id.listAction);
		View headerView = LayoutInflater.from(this).inflate(
				R.layout.layout_viewday_header, null);
		listAction.addHeaderView(headerView);

		img1 = (ImageView) findViewById(R.id.imageView1);
		img2 = (ImageView) findViewById(R.id.imageView2);
		img3 = (ImageView) findViewById(R.id.imageView3);

		img1.setVisibility(View.INVISIBLE);
		img2.setVisibility(View.INVISIBLE);
		img3.setVisibility(View.INVISIBLE);

		date = (TextView) findViewById(R.id.txtDate1);
		sum = (TextView) findViewById(R.id.txtSumMoneyDate);
		des = (TextView) findViewById(R.id.txtDescription1);
		amount = (TextView) findViewById(R.id.txtAmount);
		time = (TextView) findViewById(R.id.txtTime);

		date.setText(util.cutYear(day));
		loadFirstAction();

		if (mActionList != null) {
			sum.setText(util.convertMoney(mDAL.getSumMoneyDate(day)));
			adapter = new ActionAdapter(this, R.layout.list_action_item,
					mActionList);
			listAction = (ListView) findViewById(R.id.listAction);
			listAction.setAdapter(adapter);
		}
	}

	protected void setListeners() {
		listAction.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				des.setText(mActionList.get(arg2 - 1).getDescription());
				amount.setText(util.convertMoney(mActionList.get(arg2 - 1)
						.getAmount()));
				time.setText(String
						.valueOf(mActionList.get(arg2 - 1).getTime()));
				
				setImage(mActionList.get(arg2 - 1).getIdAction());
			}
		});

		listAction.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				mDAL.deleteAction(mActionList.get(position - 1).getIdAction());
				mActionList.remove(position - 1);
				adapter.notifyDataSetChanged();
				sum.setText(util.convertMoney(mDAL.getSumMoneyDate(day)));
				des.setText("");
				amount.setText("");
				time.setText("");
				loadFirstAction();
				return false;
			}

		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void loadData() {
		// TODO Auto-generated method stub

	}

	private void setImage(String idAction) {
		ArrayList<Bitmap> listImg = new ArrayList<Bitmap>();
		img1.setVisibility(View.INVISIBLE);
		img2.setVisibility(View.INVISIBLE);
		img3.setVisibility(View.INVISIBLE);
		if (mDAL != null) {
			listImg = mDAL.getImage(idAction);
		}
		
		if (listImg != null) {
			Log.v("list size", ""+listImg.size());
			img1.setVisibility(View.VISIBLE);
			img1.setImageBitmap(listImg.get(0));
			if (listImg.size() == 2) {
				img2.setVisibility(View.VISIBLE);
				img2.setImageBitmap(listImg.get(1));
			}
			if (listImg.size() == 3) {
				img3.setVisibility(View.VISIBLE);
				img3.setImageBitmap(listImg.get(2));
				img2.setVisibility(View.VISIBLE);
				img2.setImageBitmap(listImg.get(1));
			}
		} else {
			img1 = (ImageView) findViewById(R.id.imageView1);
			img2 = (ImageView) findViewById(R.id.imageView2);
			img3 = (ImageView) findViewById(R.id.imageView3);
		}
	}

	private void loadFirstAction() {
		des.setText(mActionList.get(0).getDescription());
		amount.setText(util.convertMoney(mActionList.get(0).getAmount()));
		time.setText(String.valueOf(mActionList.get(0).getTime()));
		setImage(mActionList.get(0).getIdAction());
	}

}
